package com.ych.modules.api.authorInfc.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.ych.modules.api.authorInfc.user.entity.ModifyUserInfoEntity;
import com.ych.modules.api.authorInfc.user.entity.ModifyUserPayPasswordEntity;
import com.ych.modules.base.dbEntity.TUserAccountChangeEntity;
import com.ych.modules.base.dbEntity.TUserInfoEntity;
import com.ych.modules.common.entity.modify.ModifyUserPwdEntity;
import com.ych.modules.common.entity.query.QueryUserAccountChangeEntity;
import com.ych.modules.common.entity.query.QueryUserInfoEntity;
import com.ych.modules.common.service.UserAccountChangeService;
import com.ych.modules.common.service.UserInfoService;
import com.ych.redis.RedisUtil;
import com.ych.redis.config.RedisKeyConfig;
import com.ych.utils.TLMap;
import com.ych.utils.mysqlPageUtil.PageUtils;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/5
 * Time: 15:12
 * Description:
 */
@RestController
@RequestMapping("/api/auth/user")
@Api(tags = {"用户相关"}, description = "用户相关")
public class UserController {

    @Autowired
    private UserInfoService          userInfoService;
    @Autowired
    private RedisUtil                redisUtil;
    @Autowired
    private UserAccountChangeService userAccountChangeService;

    @ApiOperation(value = "退出登录", position = 5)
    @PostMapping("/logout")
    public R<String> logout() {
        String userId = TLMap.getUserId();
        if (userId == null || "".equals(userId)) {
            return new R(0, "传入用户id不可为空！", null);
        }
        try {
            userInfoService.logout(userId);
        } catch (Exception e) {

        }
        try {
            SecurityUtils.getSubject().logout();
        } catch (Exception e) {

        }
        String sessionId = RedisKeyConfig.sessionToken(TLMap.getUserId());
        redisUtil.redisTemplate().delete(sessionId);
        return new R(0, "注销成功", null);
    }

    @ApiOperation(value = "获取当前登录用户信息", position = 5)
    @PostMapping("/getUserInfo")
    public R<TUserInfoEntity> getUserInfo() {
        try {
            TUserInfoEntity userInfoEntity = userInfoService.queryByUserId(TLMap.getUserId());
            return new R(0, "成功", userInfoEntity);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/modifyPwd")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    public R<String> modifyPwd(@RequestBody ModifyUserPwdEntity entity) {
        try {
            userInfoService.modifyPwd(entity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    public R<String> updateUserInfo(@RequestBody ModifyUserInfoEntity entity) {
        try {
            TUserInfoEntity userInfoEntity = JSONObject.parseObject(JSONObject.toJSONBytes(entity), TUserInfoEntity.class);
            userInfoService.apiUpdate(userInfoEntity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/listUserTeam")
    @ApiOperation(value = "查询我的团队列表", notes = "查询我的团队列表")
    public R<Page<TUserInfoEntity>> listUserTeam(@RequestBody QueryUserInfoEntity entity) {
        try {
            Page<TUserInfoEntity> page = userInfoService.listUserTeam(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }


    @PostMapping("/listUserAccountChange")
    @ApiOperation(value = "查询我的账户变更信息列表", notes = "查询我的账户变更信息列表")
    public R<Page<TUserAccountChangeEntity>> listUserAccountChange(@RequestBody QueryUserAccountChangeEntity entity) {
        try {
            entity.setUserId(TLMap.getUserId());
            Page<TUserAccountChangeEntity> page = userAccountChangeService.listUserAccountChange(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/transformGoldCoinToBrokerage")
    @ApiOperation(value = "转化金币到佣金，用于提现", notes = "转化金币到佣金，用于提现")
    public R<String> transformCoinToBrokerage(@RequestBody Integer amount) {
        try {
            String userId = TLMap.getUserId();
            userInfoService.transformGoldCoinToBrokerage(userId, new BigDecimal(String.valueOf(amount)));
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/addPayPassword")
    @ApiOperation(value = "添加支付密码", notes = "添加支付密码")
    public R<String> addPayPassword(@RequestBody ModifyUserPayPasswordEntity entity) {
        try {
            userInfoService.addPayPassword(entity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }
}

